import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class Transport implements Serializable {
    private static int id = 0;

    public static int INIT_CAPACITY;

    private List<Trajet> journey;
    private TypeTransport type;
    private int capacity;
    private int idTransport;

    public Transport(List<Trajet> journey, TypeTransport type, int capacity) {
        this.type = type;
        try {
            this.setCapacity(capacity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        this.journey = journey;
        Transport.id++;
        this.idTransport = id;
    }

    public Transport(TypeTransport type, int capacity) {
        this.type = type;
        try {
            this.setCapacity(capacity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        this.journey = new ArrayList<Trajet>();
        Transport.id++;
        this.idTransport = id;
    }

    public Transport(TypeTransport type, int capacity, int idTransport) {
        this.type = type;
        try {
            this.setCapacity(capacity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        this.journey = new ArrayList<Trajet>();
        this.idTransport = idTransport;
    }

    /**
     * @return the journey
     */
    public List<Trajet> getJourney() {
        return journey;
    }

    /**
     * @param journey
     *            the journey to set
     */
    public void setJourney(List<Trajet> journey) {
        this.journey = journey;
    }

    /**
     * @param type
     *            the type to set
     */
    public void setType(TypeTransport type) {
        this.type = type;
    }

    public void addJourney(Trajet t) {

        // Fix the free capacity
        //
        t.setFreeCapacity(getCapacity());

        // Add the trajet
        //
        journey.add(t);
    }

    public String toString() {
        String s = "ID : " + getIdTransport() + "\nType : " + type
                + "\nCapacité totale : " + getCapacity()
                + "\nTrajets possibles :\n";

        for (Trajet t : this.journey) {
            s += "\tID trajet : " + t.getIdTrajet() + " \n\t\t" + t.toString();

        }
        return s;
    }

    public TypeTransport getType() {
        return type;
    }

    /**
     * @return the capacity
     */
    public int getCapacity() {
        return capacity;
    }

    /**
     * @param capacity
     *            the capacity to set
     * @throws Exception 
     */
    public void setCapacity(int newCapacity) throws Exception {
        if (journey == null || journey.size() == 0) {
            this.capacity = newCapacity;
            INIT_CAPACITY = newCapacity;
        } else {
            int calc = newCapacity - capacity;
            if (calc > 0) {
                for (Trajet tra : journey)
                    tra.setFreeCapacity(tra.getFreeCapacity() + calc);

                this.capacity = newCapacity;
                INIT_CAPACITY = newCapacity;
            }
            else {
                throw new Exception("Possible conflit avec les capacité reservées dans les trajets");
            }
        }

    }

    public void modifyTransport(int capacity, TypeTransport type) {
        try {
            this.setCapacity(capacity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        this.setType(type);
    }

    /**
     * @return the idTransport
     */
    public int getIdTransport() {
        return idTransport;
    }

    public boolean deleteJourney(int idTrajet) throws Exception {
        for (Trajet tra : journey) {
            if (tra.getIdTrajet() == idTrajet) {
                return journey.remove(tra);
            }
        }
        throw new Exception("Invalid idTrajet");
    }
    
    public boolean equals(Object o) {
        if(this.idTransport != ((Transport)o).idTransport) {
            return false;
        }
        else if(this.capacity !=  ((Transport)o).capacity) {
            return false;
        }
        else if(!this.type.equals(((Transport)o).type)) {
            return false;
        }
        else if(!this.journey.equals(((Transport)o).journey)) {
            return false;
        }
        else{
            return true;
        }
    }
}
